package cn.changgou.controller;

import cn.changgou.entity.Result;
import cn.changgou.entity.StatusCode;
import cn.changgou.goods.pojo.Template;
import cn.changgou.service.TemplateService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/template")
@CrossOrigin
public class TemplateController {

    @Autowired
    private TemplateService templateService;

    @RequestMapping(value = "/category/{id}", method = RequestMethod.GET)
    public Result<Template> findByCategoryId(@PathVariable("id") Integer id){
        Template template = templateService.findByCategoryId(id);
        return new Result<Template>(true, StatusCode.OK, "查询成功！", template);
    }

    @RequestMapping(value = "/search/{page}/{size}", method = RequestMethod.POST)
    public Result<PageInfo> findPage(@RequestBody(required = false) Template template, @PathVariable int page, @PathVariable int size) {
        PageInfo<Template> pageInfo = templateService.findPage(template, page, size);
        return new Result(true, StatusCode.OK, "查询成功", pageInfo);
    }

    @RequestMapping(value = "/search/{page}/{size}", method = RequestMethod.GET)
    public Result<PageInfo> findPage(@PathVariable int page, @PathVariable int size) {
        PageInfo<Template> pageInfo = templateService.findPage(page, size);
        return new Result<PageInfo>(true, StatusCode.OK, "查询成功", pageInfo);
    }

    @RequestMapping(value = "/search", method = RequestMethod.POST)
    public Result<List<Template>> findList(@RequestBody(required = false) Template template) {
        List<Template> list = templateService.findList(template);
        return new Result<List<Template>>(true, StatusCode.OK, "查询成功", list);
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public Result delete(@PathVariable Integer id) {
        templateService.delete(id);
        return new Result(true, StatusCode.OK, "删除成功");
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
    public Result update(@RequestBody Template template, @PathVariable Integer id) {
        template.setId(id);
        templateService.update(template);
        return new Result(true, StatusCode.OK, "修改成功");
    }

    @RequestMapping(method = RequestMethod.POST)
    public Result add(@RequestBody Template template) {
        templateService.add(template);
        return new Result(true, StatusCode.OK, "添加成功");
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public Result<Template> findById(@PathVariable Integer id) {
        Template template = templateService.findById(id);
        return new Result<Template>(true, StatusCode.OK, "查询成功", template);
    }

    @RequestMapping(method = RequestMethod.GET)
    public Result<Template> findAll() {
        List<Template> list = templateService.findAll();
        return new Result<Template>(true, StatusCode.OK, "查询成功", list);
    }
}
